package com.wiscamp.ninechapters.problems.adapter.resources.problems;

import com.wiscamp.ninechapters.common.GlobalState;
import com.wiscamp.ninechapters.problems.application.assemblers.ProblemAssembler;
import com.wiscamp.ninechapters.problems.application.dto.commands.problems.CreateProblemCommand;
import com.wiscamp.ninechapters.problems.application.dto.commands.problems.CreateSimpleProblemCommand;
import com.wiscamp.ninechapters.problems.application.dto.commands.problems.UpdateProblemCommand;
import com.wiscamp.ninechapters.problems.application.dto.commands.problems.UpdateSimpleProblemCommand;
import com.wiscamp.ninechapters.problems.application.dto.vo.problems.ProblemKnowledgePointVO;
import com.wiscamp.ninechapters.problems.application.dto.vo.problems.ProblemVO;
import com.wiscamp.ninechapters.problems.application.services.ProblemAppService;
import com.wiscamp.ninechapters.tags.application.services.EntityAppService;
import cube.common.core.CollectionUtils;
import cube.ddd.adapter.BaseController;
import cube.ddd.adapter.RestResponse;
import cube.ddd.aspect.HandleResourceException;
import cube.ddd.common.query.PagedQuery;
import cube.ddd.utils.ResourceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;

/**
 * 题服务端点
 *
 * @author yifeng
 */
@RestController
@RequestMapping("/api/Problems")
@Tag(name = "题库-题信息")
public class ProblemController extends BaseController {
    private final ProblemAppService problemAppService;

    private final EntityAppService entityAppService;

    public ProblemController(ProblemAppService problemAppService, EntityAppService entityAppService) {
        this.problemAppService = problemAppService;
        this.entityAppService = entityAppService;
    }

    // region problems
    @GetMapping("Problems/{problemId}")
    @Operation(summary = "通过题编号获取题")
    @HandleResourceException
    public RestResponse<ProblemVO> getProblem(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                              @PathVariable("problemId") long problemId) {
        return ResourceUtils.getById(request, response, problemId, problemAppService::getProblem);
    }

    @PostMapping("Problems")
    @Operation(summary = "创建题")
    @HandleResourceException
    public RestResponse<ProblemVO> createProblem(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                 @RequestBody CreateProblemCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, problemAppService::createProblem);
    }

    @PutMapping("Problems")
    @Operation(summary = "更新题")
    @HandleResourceException
    public RestResponse<ProblemVO> updateProblem(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                 @RequestBody UpdateProblemCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, problemAppService::updateProblem);
    }

    @PostMapping("Problems/Simple")
    @Operation(summary = "创建题")
    @HandleResourceException
    public RestResponse<ProblemVO> createSimpleProblem(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                       @RequestBody CreateSimpleProblemCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, problemAppService::createSimpleProblem);
    }


    @PutMapping("Problems/Simple")
    @Operation(summary = "更新题")
    @HandleResourceException
    public RestResponse<ProblemVO> updateSimpleProblem(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                       @RequestBody UpdateSimpleProblemCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, problemAppService::updateSimpleProblem);
    }

    @PostMapping("Problems/Query")
    @Operation(summary = "分页获取题视图列表")
    @HandleResourceException
    public RestResponse<Collection<ProblemVO>> getProblemsByQuery(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                  @RequestBody PagedQuery query) {
        return ResourceUtils.getPagedObjectsByQuery(request, response, query, problemAppService::getProblemsByQuery);
    }

    @GetMapping("Problems/{problemId}/KnowledgePoints")
    @Operation(summary = "通过问题编号获取知识点列表")
    @HandleResourceException
    public RestResponse<Collection<ProblemKnowledgePointVO>> getKnowledgePointsByProblem(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                                         @PathVariable("problemId") long problemId) {
        var primaryPoints = entityAppService.getPrimaryKnowledgePointsByProblem(GlobalState.APPLICATION_ID, problemId);
        var minorPoints = entityAppService.getMinorKnowledgePointsByPrimaryKey(GlobalState.APPLICATION_ID, problemId);
        var primaryProblemPoints = CollectionUtils.toArrayList(primaryPoints, ProblemAssembler.Instance::toProblemKnowledgePoint);
        var minorProblemPoints = CollectionUtils.toArrayList(minorPoints, ProblemAssembler.Instance::toProblemKnowledgePoint);
        for (var point : primaryProblemPoints) {
            point.setPrimary(true);
        }
        for (var point : minorProblemPoints) {
            point.setPrimary(false);
        }
        var points = new ArrayList<ProblemKnowledgePointVO>();
        points.addAll(primaryProblemPoints);
        points.addAll(minorProblemPoints);
        return ResourceUtils.getOkResponse(request, response, points);
    }
    // endregion
}
